package com.virus.coronavirus.service;

import com.virus.coronavirus.error.SystemCodeEnum;
import com.virus.coronavirus.error.SystemException;
import com.virus.coronavirus.mapper.AreaMapper;
import com.virus.coronavirus.mapper.TrackMapper;
import com.virus.coronavirus.model.Area;
import com.virus.coronavirus.model.Track;
import com.virus.coronavirus.model.vo.AreaArriveVO;
import com.virus.coronavirus.model.vo.CompareVO;
import com.virus.coronavirus.model.vo.MeetVO;
import com.virus.coronavirus.model.vo.TrackVO;
import com.virus.coronavirus.utils.BmapPoint;
import com.virus.coronavirus.utils.GPS;
import com.virus.coronavirus.utils.GraphUtil;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: Bbeginner
 * @Date: 2021/2/4 13:54
 * @Version 1.0
 **/
@Service
public class TrackServiceImpl implements TrackService {

    @Autowired
    private TrackMapper trackMapper;

    @Autowired
    private AreaMapper areaMapper;


    @Override
    public List<Track> selectList() {
        return trackMapper.selectAll();
    }

    @Override
    public List<Track> findTrackBySim(Integer sim) {
        Track t = new Track();
        t.setSimulated(sim);
        List<Track> tracks = trackMapper.select(t);
        return tracks;
    }

    @Override
    public void add(TrackVO trackVO) throws SystemException {
        Track track = new Track();
        BeanUtils.copyProperties(trackVO,track);

        track.setModified_time(new Date());
        track.setCreate_time(new Date());

        /*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date time = dateFormat.parse(time1);
        track.setTrack_time(time);*/
        trackMapper.insert(track);
    }


    /**
     * 添加用户轨迹信息
     *
     * @param
     */
    /*@SneakyThrows
    @Transactional
    @Override
    public Track add(Long userId,Double lng,Double lat,String time1,String track_name) throws SystemException {
        if(userId == null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"用户ID不能为空");
        }*/

        /*Example o = new Example(Track.class);
        o.createCriteria().andEqualTo("user_id", userId);
        int i = trackMapper.selectCountByExample(o);
        if (i == 0) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR, "该用户不存在");
        }*//*
        Track track = new Track();
        track.setModified_time(new Date());
        track.setCreate_time(new Date());
        track.setLat(lat);
        track.setLng(lng);
        track.setUser_id(userId);
        track.setTrack_name(track_name);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date time = dateFormat.parse(time1);
        track.setTrack_time(time);
        trackMapper.insert(track);
//        return track;
    }*/

    /**
     * 通过轨迹id 获取单条轨迹数据
     * @param id
     * @return
     */
    @Override
    public Track findonetrack(Long id) {
        Track t = new Track();
        t.setTrack_id(id);
        Track track = trackMapper.selectOne(t);
        return track;
    }

    //得到一个人的所有轨迹
    @Override
    public List<Track> getTracksOfOne(Long id){
        Track t = new Track();
        t.setUser_id(id);
        List<Track> tracks = trackMapper.select(t);
        return tracks;
    }


    //一个人在一天至另一天内的轨迹 (单个用户) 精确到天
    @Override
    public List<Track> findTrackByTime(Long id, String t1,String t2) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time1  = dateFormat.parse(t1);
        Date time2 = dateFormat.parse(t2);
        time2.setTime(time2.getTime()+86400000);
        if (time1.after(time2)) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"时间无法逆流,我也无法帮你，(￣┰￣*)");
        }
        List<Track> tracks = getTracksOfOne(id);
        dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String tt1 = dateFormat.format(time1);
        String tt2 = dateFormat.format(time2);
        List<Track> trackList = selectTrackByTime(tracks, tt1, tt2);
        return trackList;
    }
    //一天至另一天内的轨迹(所有用户) 精确到天
    @Override
    public List<Track> findEveryonesTrackByTime(String t1,String t2) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time1  = dateFormat.parse(t1);
        Date time2 = dateFormat.parse(t2);
        time2.setTime(time2.getTime()+86400000);
        if (time2.before(time1)) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"时间无法逆流,我也无法帮你，(￣┰￣*)");
        }
        dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String tt1 = dateFormat.format(time1);
        String tt2 = dateFormat.format(time2);
        List<Track> tracks = selectTrackByTime(tt1, tt2);
        return tracks;
    }

    //一天至另一天内的轨迹(所有用户) 精确到秒
    @Override
    public List<Track> findEveryonesTrackByTime2(String t1,String t2) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date time1  = dateFormat.parse(t1);
        Date time2 = dateFormat.parse(t2);
        time2.setTime(time2.getTime()+86400000);
        if (time2.before(time1)) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"时间无法逆流,我也无法帮你，(￣┰￣*)");
        }
        dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String tt1 = dateFormat.format(time1);
        String tt2 = dateFormat.format(time2);
        List<Track> tracks = selectTrackByTime(tt1, tt2);
        return tracks;
    }

    //一天至另一天内的轨迹(单个用户) 精确到秒
    @Override
    public List<Track> findonesTrackByTime2(Long id,String t1, String t2) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date time1  = dateFormat.parse(t1);
        Date time2 = dateFormat.parse(t2);
        time2.setTime(time2.getTime()+86400000);
        if (time2.before(time1)) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"时间无法逆流,我也无法帮你，(￣┰￣*)");
        }
        dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String tt1 = dateFormat.format(time1);
        String tt2 = dateFormat.format(time2);

        Track t = new Track();
        t.setUser_id(id);
        List<Track> tracks = trackMapper.select(t);
        List<Track> trackList = selectTrackByTime(tracks, tt1, tt2);
        return trackList;
    }

    @Override
    public List<Track> findOnesTrackSomeday(Long id, String time) throws Exception {
        List<Track> tracks = findTrackByTime(id, time,time);
        return tracks;
    }



    @Override
    public Area findarea(Long id) {
        Area a = new Area();
        a.setId(id);
        Area area = areaMapper.selectOne(a);
        return area;
    }


    //一个用户在几天内的轨迹 (往前数几天)
    @Override
    public List<Track> findTrackById(Long id,Integer days) {
        List<Track> tracks = getTracksOfOne(id);
        Calendar c = Calendar.getInstance();
        Date time1 = c.getTime();//当前时间
        c.add(Calendar.DAY_OF_MONTH,-days);
        Date time2 = c.getTime();//几天前时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String t1 = dateFormat.format(time1);
        String t2 = dateFormat.format(time2);
        tracks = selectTrackByTime(tracks, t2,t1);
        return tracks;
    }


    //以下两个“selectTrackByTime”都不进行直接调用,查询使用了一个findEveryonesTrackByTime
    //一段时间内的给定集合内的轨迹；
    @SneakyThrows
    @Override
    public List<Track> selectTrackByTime(List<Track> tracks,String t1,String t2) {
        List<Track> trackList = new ArrayList<Track>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date time1 = dateFormat.parse(t1);
        Date time2 = dateFormat.parse(t2);
        for (Track track:tracks){
            Date time = track.getTrack_time();
            if (time.after(time1)){
                trackList.add(track);
            }
            if (time.after(time2)){
                trackList.remove(track);
            }
        }
        return trackList;
    }

    /**
     * 一段时间内所有用户的信息
     * @param time1
     * @param time2
     * @return
     */
    public List<Track> selectTrackByTime(String time1,String time2){
        List<Track> tracks = trackMapper.selectAll();
        List<Track> trackList = selectTrackByTime(tracks, time1, time2);
        return trackList;
    }

    @Override
    public Track update(Long trackid,Double lat,Double lng,String track_time,String track_name) throws Exception {

        Track track = trackMapper.selectByPrimaryKey(trackid);
        if (track== null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要更新的轨迹不存在");
        }
        track.setLat(lat);
        track.setLng(lng);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dateFormat.parse(track_time);
        track.setTrack_time(date);
        track.setTrack_name(track_name);
        trackMapper.updateByPrimaryKey(track);
        return track;
    }



    // 一个范围内全部的轨迹 (被调用的子方法)
    @Override
    public List<Track> selectTrackByPlace(List<Track> tracks, Double lng1, Double lat1, Double lng2, Double lat2) {
        List<Track> trackList = new ArrayList<Track>();
        for (Track track:tracks){
            Double lng0 = track.getLng();
            Double lat0 = track.getLat();
            if (lng0>lng1){
                if (lng0<lng2) {
                    trackList.add(track);
                }
            }
            if (lat0<lat1){
                trackList.remove(track);
            }else if (lat0>lat2){
                trackList.remove(track);
            }
        }
        return trackList;
    }

    /**
     * 一个地点范围内所有的用户轨迹 (只是一个点,两点确定一条直线)
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     */
    @Override
    public List<Track> trackplaceall(Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> tracks = trackMapper.selectAll();
        List<Track> trackList = selectTrackByPlace(tracks, lng1,lat1,lng2,lat2);
        return trackList;
    }

    /**
     * 通过Id查一个地点范围内的用户轨迹
     * @param id
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     */
    @Override
    public List<Track> trackplaceid(Long id, Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> tracks = getTracksOfOne(id);
        List<Track> trackList = selectTrackByPlace(tracks, lng1,lat1,lng2,lat2);
        return trackList;
    }

    /**
     * 查某天去过某一个地点的用户轨迹
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     */
    @Override
    public List<Track> tracktimeplace(String time, Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> tracks = findEveryonesTrackByTime(time,time);
        List<Track> trackList = selectTrackByPlace(tracks, lng1,lat1,lng2,lat2);
        return trackList;
    }

    @Override
    public List<Track> tracktime1time2place(String time1,String time2, Double lng1, Double lat1, Double lng2, Double lat2) throws Exception {
        List<Track> tracks = findEveryonesTrackByTime(time1,time2);
        List<Track> trackList = selectTrackByPlace(tracks, lng1,lat1,lng2,lat2);
        return trackList;
    }


    @Override
    public double findone(Long id1, Long id2) {
        Track track1 = findonetrack(id1);
        Track track2 = findonetrack(id2);
        Double lat1 = track1.getLat();
        Double lng1 = track1.getLng();
        Double lat2 = track2.getLat();
        Double lng2 = track2.getLng();
        double distance = GPS.GetDistance(lat1, lng1, lat2, lng2);
        return distance;
    }

    // 单条轨迹对应单个区域
    @Override
    public Boolean adjust(Long idt, Long ida) {
        Track track = findonetrack(idt);
        Double gplat = track.getLat();
        Double gplng = track.getLng();

        Area area = findarea(ida);
        String rlng=area.getRlng();
        String rlat=area.getRlat();
        String[] listrlng=rlng.split(",");
        String[] listrlat=rlat.split(",");
        Double[] listrlngs= (Double[]) ConvertUtils.convert(listrlng,Double.class);
        Double[] listrlats= (Double[]) ConvertUtils.convert(listrlat,Double.class);
        BmapPoint[] boundaryBmapPoints = new BmapPoint[listrlngs.length];
        for(int i=0;i<listrlngs.length;i++){
            Double lat=listrlats[i];
            Double lng=listrlngs[i];
            boundaryBmapPoints[i]=new BmapPoint(lng,lat);
        }
//        System.out.println(boundaryBmapPoints[1]);

//        Double lat1 = area.getLat1();
//        Double lat2 = area.getLat2();
//        Double lat3 = area.getLat3();
//        Double lat4 = area.getLat4();
//        Double lng1 = area.getLng1();
//        Double lng2 = area.getLng2();
//        Double lng3 = area.getLng3();
//        Double lng4 = area.getLng4();
//        BmapPoint[] boundaryBmapPoints = new BmapPoint[] { new BmapPoint(lng1 , lat1), new BmapPoint(lng2, lat2), new BmapPoint(lng3 , lat3), new BmapPoint(lng4 , lat4) };
        BmapPoint n1 = new BmapPoint(gplng , gplat);
        boolean pointInPolygon = GraphUtil.isPointInPolygon(n1, boundaryBmapPoints);
        return pointInPolygon;
    }

    @Override
    public List<AreaArriveVO> inArea(List<Track> tracks1, Area area) {
        ArrayList<AreaArriveVO> arriveVOList = new ArrayList<>();
        String rlng=area.getRlng();
        String rlat=area.getRlat();
        String placename = area.getPlacename();
        String[] listrlng=rlng.split(",");
        String[] listrlat=rlat.split(",");
        Double[] listrlngs= (Double[]) ConvertUtils.convert(listrlng,Double.class);
        Double[] listrlats= (Double[]) ConvertUtils.convert(listrlat,Double.class);
        BmapPoint[] boundaryBmapPoints = new BmapPoint[listrlngs.length];
        for(int k=0;k<listrlngs.length;k++){
            Double lat=listrlats[k];
            Double lng=listrlngs[k];
            boundaryBmapPoints[k]=new BmapPoint(lng,lat);
        }
        for (int j = 0; j < Long.MAX_VALUE; j++) {
            try {
                AreaArriveVO arrive = new AreaArriveVO();
                Track track1 = tracks1.get(j);
                Double lat1 = track1.getLat();
                Double lng1 = track1.getLng();
                Date track_time1 = track1.getTrack_time();
                Long user_id = track1.getUser_id();
//                        long time1 = track_time1.getTime();
                String track_name1 = track1.getTrack_name();
                BmapPoint n1 = new BmapPoint(lng1 , lat1);
                boolean pointInPolygon = GraphUtil.isPointInPolygon(n1, boundaryBmapPoints);
                if(pointInPolygon){
                    arrive.setPlacename(placename);
                    arrive.setUser_id(user_id);
                    arrive.setTrack_time(track_time1);
                    arrive.setTrack_name(track_name1);
                    arriveVOList.add(arrive);
                } else {
//                            System.out.println("没有数据");
                }
            } catch (Exception e) {
                break;
            }
        }
        return arriveVOList;
    }

    /**
     * 判断逻辑是,如果去过风险区域,则为true,返回具体地点
     * 如果没有去过,则返回没有去过,就是list = null
     * 每个风险区域都有时限,后期增加时间限制
     */

    // 单个区域对应多条轨迹[单个用户](对指定的地点,排查一个用户是否去过)[建立新的VO] 我的
    @Override
    public List<AreaArriveVO> onetoone(Long id, Long ida) {
        List<Track> tracks1 = getTracksOfOne(id);
        Area area = findarea(ida);
        /*ArrayList<AreaArriveVO> arriveVOList = new ArrayList<>();
        String rlng=area.getRlng();
        String rlat=area.getRlat();
        String placename = area.getPlacename();
        String[] listrlng=rlng.split(",");
        String[] listrlat=rlat.split(",");
        Double[] listrlngs= (Double[]) ConvertUtils.convert(listrlng,Double.class);
        Double[] listrlats= (Double[]) ConvertUtils.convert(listrlat,Double.class);
        BmapPoint[] boundaryBmapPoints = new BmapPoint[listrlngs.length];
        for(int k=0;k<listrlngs.length;k++){
            Double lat=listrlats[k];
            Double lng=listrlngs[k];
            boundaryBmapPoints[k]=new BmapPoint(lng,lat);
        }
        for (int j = 0; j < 100; j++) {
            try {
                AreaArriveVO arrive = new AreaArriveVO();
                Track track1 = tracks1.get(j);
                Double lat1 = track1.getLat();
                Double lng1 = track1.getLng();
                Date track_time1 = track1.getTrack_time();
                Long user_id = track1.getUser_id();
//                        long time1 = track_time1.getTime();
                String track_name1 = track1.getTrack_name();
                BmapPoint n1 = new BmapPoint(lng1 , lat1);
                boolean pointInPolygon = GraphUtil.isPointInPolygon(n1, boundaryBmapPoints);
                if(pointInPolygon){
                    arrive.setPlacename(placename);
                    arrive.setUser_id(user_id);
                    arrive.setTrack_time(track_time1);
                    arrive.setTrack_name(track_name1);
                    arriveVOList.add(arrive);
                } else {
//                            System.out.println("没有数据");
                }
            } catch (Exception e) {
                break;
            }
        }*/
        List<AreaArriveVO> arriveVOList = inArea(tracks1, area);
        return arriveVOList;
    }

    // 单个区域对应多条轨迹[多个用户](对指定的地点,排查所有用户是否去过)[后期修改为指定条件] 伟卓
    @Override
    public List<AreaArriveVO> onetomuch(Long ida) {
        List<Track> tracks1 = trackMapper.selectAll(); //就这个不同,一个在用户的轨迹遍历,一个所有用户遍历
        Area area = findarea(ida);
        /*ArrayList<AreaArriveVO> arriveVOList = new ArrayList<>();
        String rlng=area.getRlng();
        String rlat=area.getRlat();
        String placename = area.getPlacename();
        String[] listrlng=rlng.split(",");
        String[] listrlat=rlat.split(",");
        Double[] listrlngs= (Double[]) ConvertUtils.convert(listrlng,Double.class);
        Double[] listrlats= (Double[]) ConvertUtils.convert(listrlat,Double.class);
        BmapPoint[] boundaryBmapPoints = new BmapPoint[listrlngs.length];
        for(int k=0;k<listrlngs.length;k++){
            Double lat=listrlats[k];
            Double lng=listrlngs[k];
            boundaryBmapPoints[k]=new BmapPoint(lng,lat);
        }
        for (int j = 0; j < 100; j++) {
            try {
                AreaArriveVO arrive = new AreaArriveVO();
                Track track1 = tracks1.get(j);
                Double lat1 = track1.getLat();
                Double lng1 = track1.getLng();
                Date track_time1 = track1.getTrack_time();
                Long user_id = track1.getUser_id();
//                        long time1 = track_time1.getTime();
                String track_name1 = track1.getTrack_name();
                BmapPoint n1 = new BmapPoint(lng1 , lat1);
                boolean pointInPolygon = GraphUtil.isPointInPolygon(n1, boundaryBmapPoints);
                if(pointInPolygon){
                    arrive.setPlacename(placename);
                    arrive.setUser_id(user_id);
                    arrive.setTrack_time(track_time1);
                    arrive.setTrack_name(track_name1);
                    arriveVOList.add(arrive);
                } else {
//                            System.out.println("没有数据");
                }
            } catch (Exception e) {
                break;
            }
        }*/
        List<AreaArriveVO> arriveVOList = inArea(tracks1, area);
        return arriveVOList;
    }

    // 多个区域对应多条轨迹[单个用户](这个人有没有经过风险区域) 梦迪
    @Override
    public List<AreaArriveVO> muchtoone(Long id) {
        List<Track> tracks1 = getTracksOfOne(id);
        ArrayList<AreaArriveVO> arriveVOList = new ArrayList<>();
        List<Area> areas = areaMapper.selectAll();
        for (int i = 0; i < 100; i++) {
            try {
                Area area = areas.get(i);
                /*String rlng=area.getRlng();
                String rlat=area.getRlat();
                String placename = area.getPlacename();
                String[] listrlng=rlng.split(",");
                String[] listrlat=rlat.split(",");
                Double[] listrlngs= (Double[]) ConvertUtils.convert(listrlng,Double.class);
                Double[] listrlats= (Double[]) ConvertUtils.convert(listrlat,Double.class);
                BmapPoint[] boundaryBmapPoints = new BmapPoint[listrlngs.length];
                for(int k=0;k<listrlngs.length;k++){
                    Double lat=listrlats[k];
                    Double lng=listrlngs[k];
                    boundaryBmapPoints[k]=new BmapPoint(lng,lat);
                }
                for (int j = 0; j < 100; j++) {
                    try {
                        AreaArriveVO arrive = new AreaArriveVO();
                        Track track1 = tracks1.get(j);
                        Double lat1 = track1.getLat();
                        Double lng1 = track1.getLng();
                        Date track_time1 = track1.getTrack_time();
                        Long user_id = track1.getUser_id();
//                        long time1 = track_time1.getTime();
                        String track_name1 = track1.getTrack_name();
                        BmapPoint n1 = new BmapPoint(lng1 , lat1);
                        boolean pointInPolygon = GraphUtil.isPointInPolygon(n1, boundaryBmapPoints);
                        if(pointInPolygon){
                            arrive.setPlacename(placename);
                            arrive.setUser_id(user_id);
                            arrive.setTrack_time(track_time1);
                            arrive.setTrack_name(track_name1);
                            arriveVOList.add(arrive);
                        } else {
//                            System.out.println("没有数据");
                        }
                    } catch (Exception e) {
                        break;
                    }
                }*/
                List<AreaArriveVO> arriveVOLis1 = inArea(tracks1, area);
                arriveVOList.addAll(arriveVOLis1);
            } catch (Exception e) {
                break;
            }
        }
       return arriveVOList;
    }

    // 多个区域对应多条轨迹[多个用户](所有用户中有那些人经过了风险区域) My
    @Override
    public List<AreaArriveVO> muchtomuch() {
        List<Track> tracks1 = trackMapper.selectAll();
        ArrayList<AreaArriveVO> arriveVOList = new ArrayList<>();
        List<Area> areas = areaMapper.selectAll();
        for (int i = 0; i < 100; i++) {
            try {
                Area area = areas.get(i);
                /*String rlng=area.getRlng();
                String rlat=area.getRlat();
                String placename = area.getPlacename();
                String[] listrlng=rlng.split(",");
                String[] listrlat=rlat.split(",");
                Double[] listrlngs= (Double[]) ConvertUtils.convert(listrlng,Double.class);
                Double[] listrlats= (Double[]) ConvertUtils.convert(listrlat,Double.class);
                BmapPoint[] boundaryBmapPoints = new BmapPoint[listrlngs.length];
                for(int k=0;k<listrlngs.length;k++){
                    Double lat=listrlats[k];
                    Double lng=listrlngs[k];
                    boundaryBmapPoints[k]=new BmapPoint(lng,lat);
                }
                for (int j = 0; j < 100; j++) {
                    try {
                        AreaArriveVO arrive = new AreaArriveVO();
                        Track track1 = tracks1.get(j);
                        Double lat1 = track1.getLat();
                        Double lng1 = track1.getLng();
                        Date track_time1 = track1.getTrack_time();
                        Long user_id = track1.getUser_id();
//                        long time1 = track_time1.getTime();
                        String track_name1 = track1.getTrack_name();
                        BmapPoint n1 = new BmapPoint(lng1 , lat1);
                        boolean pointInPolygon = GraphUtil.isPointInPolygon(n1, boundaryBmapPoints);
                        if(pointInPolygon){
                            arrive.setPlacename(placename);
                            arrive.setUser_id(user_id);
                            arrive.setTrack_time(track_time1);
                            arrive.setTrack_name(track_name1);
                            arriveVOList.add(arrive);
                        } else {
//                            System.out.println("没有数据");
                        }
                    } catch (Exception e) {
                        break;
                    }
                }
*/
                List<AreaArriveVO> arriveVOLis1 = inArea(tracks1, area);
                arriveVOList.addAll(arriveVOLis1);
            } catch (Exception e) {
                break;
            }
        }
        return arriveVOList;
    }


    // 现阶段风险区域是公布的,之后可以修改为算法预测出来的区域

    @Override
    public Area findonearea(Long id) {
        Area t = new Area();
        t.setId(id);
        Area area = areaMapper.selectOne(t);
        return area;
    }

    @Override
    public Area findAreabyname(String name) {
        /*//只会返回一个表示经纬度坐标的字符串
        Example example = new Example(Area.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("placename", "%" + name + "%");
        Area area = areaMapper.selectOneByExample(example);
        return area;*/
        Example example = new Example(Area.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("placename","%"+name+"%");
//        List<Area> areas = areaMapper.selectByExample(example);
        return areaMapper.selectOneByExample(example);
    }

    // 判断周边的风险区域, 返回全部的
    @Override
    public List<Double> allareadis(Long id1) {
        Track track = findonetrack(id1);
        Double lat = track.getLat();
        Double lng = track.getLng();

        ArrayList<Double> distanceVOS = new ArrayList<>();
//        List<Track> tracksOfOne = getTracksOfOne(id1);
        List<Area> areas = areaMapper.selectAll();
        for (int i = 0; i < 100; i++) {
            try {
                Area area = areas.get(i);
                Double clat = area.getClat();
                Double clng = area.getClng();
               /* System.out.println(lat);
                System.out.println(lng);*/
                double distance = GPS.GetDistance(lat, lng, clat, clng);
                distanceVOS.add(distance);
//                System.out.println(distance);
//                return distanceVOS;
            } catch (Exception e) {
//                System.out.println(i + 1);
                break;
            }
        }
//        return distance;
//        return null;
        return distanceVOS;
    }

    // 根据距离,返回周边的风险区域
    @Override
    public List<Double> areadis(Long id1, Double dis) {
        Track track = findonetrack(id1);
        Double lat = track.getLat();
        Double lng = track.getLng();

        ArrayList<Double> distanceVOS = new ArrayList<>();
//        List<Track> tracksOfOne = getTracksOfOne(id1);
        List<Area> areas = areaMapper.selectAll();
        for (int i = 0; i < 100; i++) {
            try {
                Area area = areas.get(i);
                Double clat = area.getClat();
                Double clng = area.getClng();
               /* System.out.println(lat);
                System.out.println(lng);*/
                double distance = GPS.GetDistance(lat, lng, clat, clng);
                if (distance < dis) {
                    distanceVOS.add(distance);
                }
//                System.out.println(distance);
//                return distanceVOS;
            } catch (Exception e) {
//                System.out.println(i + 1);
                break;
            }
        }
//        return distance;
//        return null;
        return distanceVOS;
    }

    // 字符拼接
    @Override
    public List<String> areanamedis (Long id1, Double dis) {
        Track track = findonetrack(id1);
        Double lat = track.getLat();
        Double lng = track.getLng();

        ArrayList<String> distanceVOS = new ArrayList<>();
        List<Area> areas = areaMapper.selectAll();
        for (int i = 0; i < 100; i++) {
            try {
                Area area = areas.get(i);
                Double clat = area.getClat();
                Double clng = area.getClng();
                String placename = area.getPlacename();
                double distance = GPS.GetDistance(lat, lng, clat, clng);
                if (distance < dis) {
                    String areadis = Double.toString(distance);
                    String track_dis = new StringBuilder(placename).append("  距离为:").append(areadis).toString();
                /*distanceVOS.add(track_name);
                distanceVOS.add(dis);*/
                    distanceVOS.add(track_dis);
                }
            } catch (Exception e) {
                break;
            }
        }
        return distanceVOS;
    }

    // 用户的名字日后考虑
    // 之后考虑是否使用Cache或者存入数据库
    @Override
    public List<CompareVO> areadistance(Long id1, Double dis) {
        Track track = findonetrack(id1);
        Double lat = track.getLat();
        Double lng = track.getLng();
        String track_name = track.getTrack_name();

//        ArrayList<String> distanceVOS = new ArrayList<>();

        List<CompareVO> compareVOList = new ArrayList<CompareVO>();

        List<Area> areas = areaMapper.selectAll();
        for (int i = 0; i < 100; i++) {
            try {
                CompareVO compare = new CompareVO();
                Area area = areas.get(i);
                Double clat = area.getClat();
                Double clng = area.getClng();
                String placename = area.getPlacename();
                double distance = GPS.GetDistance(lat, lng, clat, clng);
                if (distance < dis) {
                    compare.setTrack_id(id1);
                    compare.setTrack_name(track_name);
                    compare.setPlacename(placename);
                    compare.setDistance(distance);

//                    System.out.println(compare);

                    compareVOList.add(compare);
            }
            } catch (Exception e) {
                break;
            }
        }
//        return compare;
        return compareVOList;
    }


    // 与病患轨迹的比较, 现在是单纯的比较距离 之后要比较时间范围
    // 解决重复比较 正反, 反正的问题  12,21是同种情况, 直接另j=i

    //是否相遇, 相遇几次暂时没有输出
    @Override
    public Boolean userconflict(Long id1, Long id2, Double dis, long time) {
        List<Track> tracks1 = getTracksOfOne(id1);
        List<Track> tracks2 = getTracksOfOne(id2);
        Integer num = 0;
//        Boolean flag = false;

        long day = 0;
        long hour = 0;
        long diff;

        for (int i = 0; i < 100; i++) {
            try {
                Track track1 = tracks1.get(i);
                Double lat1 = track1.getLat();
                Double lng1 = track1.getLng();
                Date track_time1 = track1.getTrack_time();
                long time1 = track_time1.getTime();
                for (int j = i; j < 100; j++) {
                    try {
                        Track track2 = tracks2.get(j);
                        Double lat2 = track2.getLat();
                        Double lng2 = track2.getLng();
                        Date track_time2 = track2.getTrack_time();
                        long time2 = track_time2.getTime();
                        if (time1 > time2){
                            diff = time1-time2;
                        } else {
                            diff = time2-time1;
                        }
                        hour = diff / (60 * 60 * 1000);
                        if (hour < time) {
                            double distance = GPS.GetDistance(lat1, lng1, lat2, lng2);
                            if (distance < dis) {
//                                System.out.println("true");
                                num = num+1;
                            } else {
//                            System.out.println("false");
//                            return false;
                            }
                        }
                    } catch (Exception e) {
//                        System.out.println(j + 1);
                        break;
                    }
                }
            } catch (Exception e){
//                System.out.println(i+1);
                break;
            }
        }
        if (num > 0){
            System.out.println(num);
            return true;
//            flag = true;
        }
//        return flag;
        return null;
    }

    //在哪里相遇,字符拼接
    @Override
    public List<String> usertrack(Long id1, Long id2, Double dis, long time) {
        List<Track> tracks1 = getTracksOfOne(id1);
        List<Track> tracks2 = getTracksOfOne(id2);
        ArrayList<String> usertracks = new ArrayList<>();

        long day = 0;
        long hour = 0;
        long diff;

        for (int i = 0; i < 100; i++) {
            try {
                Track track1 = tracks1.get(i);
                Double lat1 = track1.getLat();
                Double lng1 = track1.getLng();
                Date track_time1 = track1.getTrack_time();
                long time1 = track_time1.getTime();
                String track_name1 = track1.getTrack_name();
                for (int j = i; j < 100; j++) {
                    try {
                        Track track2 = tracks2.get(j);
                        Double lat2 = track2.getLat();
                        Double lng2 = track2.getLng();
                        Date track_time2 = track2.getTrack_time();
                        long time2 = track_time2.getTime();
                        String track_name2 = track2.getTrack_name();
                        if (time1 > time2){
                            diff = time1-time2;
                        } else {
                            diff = time2-time1;
                        }
                        day = diff / (24 * 60 * 60 * 1000);
//                        hour = (diff / (60 * 60 * 1000) - day * 24);
                        hour = diff / (60 * 60 * 1000);
                       /* System.out.println(track_name1);
                        System.out.println(track_name2);
                        System.out.println(hour);*/
                       if (hour < time) {
                           double distance = GPS.GetDistance(lat1, lng1, lat2, lng2);
                           if (distance < dis) {
//                            System.out.println("true");
                               String userdis = Double.toString(distance);
                               String track_dis = new StringBuilder("您曾经去过的   ").append(track_name1).append("   与病患曾去过的   ").append(track_name2).append("  距离为:").append(userdis).toString();
                               usertracks.add(track_dis);
                           } else {
//                            System.out.println("false");
//                            return false;
                           }
                       }
                    } catch (Exception e) {
//                        System.out.println(j + 1);
                        break;
                    }
                }
            } catch (Exception e){
//                System.out.println(i+1);
                break;
            }
        }
        return usertracks;
    }

    @Override
    public List<MeetVO> useraction(Long id1, Long id2, Double dis, long time) {
        List<Track> tracks1 = getTracksOfOne(id1);
        List<Track> tracks2 = getTracksOfOne(id2);
//        ArrayList<String> usertracks = new ArrayList<>();
        List<MeetVO> meetVOList = new ArrayList<MeetVO>();

        long day = 0;
        long hour = 0;
        long diff;

        for (int i = 0; i < 100; i++) {
            try {
                MeetVO meet = new MeetVO();
                Track track1 = tracks1.get(i);
                Double lat1 = track1.getLat();
                Double lng1 = track1.getLng();
                Date track_time1 = track1.getTrack_time();
                long time1 = track_time1.getTime();
                String track_name1 = track1.getTrack_name();
                for (int j = i; j < 100; j++) {
                    try {
                        Track track2 = tracks2.get(j);
                        Double lat2 = track2.getLat();
                        Double lng2 = track2.getLng();
                        Date track_time2 = track2.getTrack_time();
                        long time2 = track_time2.getTime();
                        String track_name2 = track2.getTrack_name();
                        if (time1 > time2){
                            diff = time1-time2;
                        } else {
                            diff = time2-time1;
                        }
                        day = diff / (24 * 60 * 60 * 1000);
//                        hour = (diff / (60 * 60 * 1000) - day * 24);
                        hour = diff / (60 * 60 * 1000);
                       /* System.out.println(track_name1);
                        System.out.println(track_name2);
                        System.out.println(hour);*/
                        if (hour < time) {
                            double distance = GPS.GetDistance(lat1, lng1, lat2, lng2);
                            if (distance < dis) {
                                /*String userdis = Double.toString(distance);
                                String track_dis = new StringBuilder("您曾经去过的   ").append(track_name1).append("   与病患曾去过的   ").append(track_name2).append("  距离为:").append(userdis).toString();
                                usertracks.add(track_dis);*/
                                meet.setTrack_name1(track_name1);
                                meet.setTrack_name2(track_name2);
                                meet.setDistance(distance);
                                meet.setLat1(lat1);
                                meet.setLng1(lng1);
                                meet.setLat2(lat2);
                                meet.setLng2(lng2);
                                meetVOList.add(meet);
                            } else {
                            }
                        }
                    } catch (Exception e) {
                        break;
                    }
                }
            } catch (Exception e){
//                System.out.println(i+1);
                break;
            }
        }
        return meetVOList;
}







    /*@Override
    public Boolean distance(Long id1, Long id2) {
        Double distance = prework(id1, id2);
        if (distance < 10){
            return true;
        } else {
            return false;
        }
    }*/
}
